package org.mspring.platform.qiniu;

import org.apache.commons.lang3.StringUtils;

import com.qiniu.api.fop.ImageView;

/**
 *
 * @since 2014年5月9日
 * @author Gao Youbo
 */
public class QiniuImageStyleUtils {

    public static final String DEFAULT_SPLIT = "/";

    /**
     * 根据图片样式名称设置图片样式
     *
     * @param url
     * @param styleName
     * @return
     * @throws IllegalArgumentException
     */
    public static String generateByStyleName(String url, String styleName) throws IllegalArgumentException {
        return generateByStyleName(url, styleName, DEFAULT_SPLIT);
    }

    /**
     * 根据图片样式名称设置图片样式
     *
     * @param url
     *            图片原URL
     * @param styleName
     *            样式名称
     * @param split
     *            分隔符
     * @return
     * @throws IllegalArgumentException
     */
    public static String generateByStyleName(String url, String styleName, String split) throws IllegalArgumentException {
        if (StringUtils.isBlank(url)) {
            throw new IllegalArgumentException("url不能为空");
        }
        if (StringUtils.isBlank(styleName)) {
            throw new IllegalArgumentException("styleName不能为空");
        }
        if (StringUtils.isBlank(split)) {
            throw new IllegalArgumentException("split不能为空");
        }
        StringBuilder sb = new StringBuilder();
        sb.append(url).append(split).append(styleName);
        return sb.toString();
    }

    /**
     * 根据ImageView生成图片
     *
     * 默认mode=2
     *
     * @param url
     * @param width
     * @param height
     * @return
     * @throws IllegalArgumentException
     */
    public static String generateByImageView(String url, int width, int height) throws IllegalArgumentException {
        return generateByImageView(url, 2, width, height, 0);
    }

    /**
     * 根据ImageView生成图片
     *
     * @param url
     * @param mode
     * @param width
     * @param height
     * @return
     * @throws IllegalArgumentException
     */
    public static String generateByImageView(String url, int mode, int width, int height) throws IllegalArgumentException {
        return generateByImageView(url, mode, width, height, 0);
    }

    /**
     * 根据ImageView生成图片
     *
     * @param url
     * @param mode
     * @param width
     * @param height
     * @param quality
     * @return
     * @throws IllegalArgumentException
     */
    public static String generateByImageView(String url, int mode, int width, int height, int quality) throws IllegalArgumentException {
        ImageView imageView = new ImageView();
        imageView.mode = mode;
        imageView.width = width;
        imageView.height = height;
        imageView.quality = quality;
        return generateByImageView(url, imageView);
    }

    /**
     * 根据ImageView生成图片
     *
     * @param url
     * @param imageView
     * @return
     * @throws IllegalArgumentException
     */
    public static String generateByImageView(String url, ImageView imageView) throws IllegalArgumentException {
        if (StringUtils.isBlank(url)) {
            throw new IllegalArgumentException("url不能为空");
        }
        return imageView.makeRequest(url);
    }

}
